#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "FrameProvider.h"
#include "Decoder.h"
#include <QStandardPaths>
#include "AudioPlayer.h"
#include "MediaInspector.h"
#include "SystemClockSynchronizer.h"
#include <QQuickWindow>
#include <QSGRendererInterface>
#include "FrameRenderer.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);

    QQmlApplicationEngine engine;
    const QUrl url("qrc:/BNSPlayer/App.qml");
    QObject::connect(
                &engine, &QQmlApplicationEngine::objectCreated, &app,
                [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    },
    Qt::QueuedConnection);

    qmlRegisterType<FrameProvider>("BNSPlayer", 1, 0, "VideoFrameProvider");
    qmlRegisterType<Decoder>("BNSPlayer", 1, 0, "Decoder");
    qmlRegisterType<AudioPlayer>("BNSPlayer", 1, 0, "AudioPlayer");
    qmlRegisterInterface<ISynchronizer>("BNSPlayer", 1);
    qmlRegisterType<MediaInspector>("BNSPlayer", 1, 0, "MediaInspector");
    qmlRegisterType<SystemClockSynchronizer>("BNSPlayer", 1, 0, "SystemClockSynchronizer");
    qmlRegisterType<FrameRenderer>("BNSPlayer", 1, 0, "FrameRenderer");

    engine.addImportPath("qrc:/qml");
    engine.setOfflineStoragePath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));

    engine.load(url);

    if (engine.rootObjects().isEmpty()) {
        return -1;
    }

    return app.exec();
}
